<?php
class PublicAction extends BaseAction
{
	public function _initialize()
	{
		parent::_initialize();
	}

	public function login(){
		$uid = getLoginUserInfo();
		if(!empty($uid)){
			$this->error('已经登陆过！');
		}
		if($this->isPost()){
			$email = $this->_post('email');
			$pwd = md5($this->_post('pwd'));
			$user = $this->checkLogin($email, $pwd);
			if($user){
				$uid = $user['id'];
                setLoginUserInfo($uid);
                //注册成功修改，在线状态。
                setOnline($uid);
                //记录登陆日志
                loginLog($uid);
                //记录登陆次数
                addUserLoginCount($uid);
			}else{
				
			}
		}
		$this->display();
	}
	
	/**
	 * 检查登录信息是否正确
	 * @param String $email 用户登录邮箱
	 * @param String $pas 用户密码
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	protected function checkLogin($email, $pwd = null)
	{
		$user = array();
		$map = array();
		$UserModel = M('Users');
		$field = 'id,name,email,status';
		if ($email && $pwd) {
			$map['email'] = $email;
			$map['pwd'] = $pwd;
		} else if ($email) {
			$map['email'] = $email;
		} else {
			return false;
		}
		$user = $UserModel->where($map)->field($field)->find();
		if ($user) {
			return $user;
		}
		return false;
	}
	
	/**
	 * 退出操作
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function loginOut(){
		if (isset($_SESSION[C('USER_AUTH_KEY')])) {
			unset($_SESSION);
			session_destroy();
			$this->assign('jumpUrl', U('/'));
			$this->success('成功退出！');
		}
	}
}

?>